package com.testing.sunnycommon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.testing.sunnycommon.automation.ExcelUtils;
import com.testing.sunnycommon.mapper.SunnyResultMapper;
import com.testing.sunnycommon.model.SunnyResult;
import com.testing.sunnycommon.model.SunnyResultDetail;
import com.testing.sunnycommon.service.SunnyResultDetailService;
import com.testing.sunnycommon.service.SunnyResultService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Classname SunnyResultServiceImpl
 * @Description 类型说明
 * @Date 2023/5/29 21:42
 * @Created by 特斯汀Roy
 */
@Service
public class SunnyResultServiceImpl extends ServiceImpl<SunnyResultMapper, SunnyResult> implements SunnyResultService {
    @Autowired
    SunnyResultMapper sunnyResultMapper;

    @Autowired
    SunnyResultDetailService sunnyResultDetailService;

    @Override
    public SunnyResult getLastSunnyResult() {
        return getOne(new LambdaQueryWrapper<SunnyResult>()
                .orderByDesc(SunnyResult::getId)
                .last("limit 1"));
    }

    @Override
    public Long getMaxBuildOrderOfProject(Long projectId) {
        Long maxBuildOrderOfProject = sunnyResultMapper.getMaxBuildOrderOfProject(projectId);
        return maxBuildOrderOfProject == null ? 0 : maxBuildOrderOfProject;
    }

    /**
     * 生成结果的excel格式文件
     *
     * @param resultId 需要导出的执行结果
     * @return  返回xssfworkbook类型的excel格式对象
     */
    @Override
    public XSSFWorkbook exportResult(Long resultId) {
        List<SunnyResultDetail> sunnyResultDetails = sunnyResultDetailService.list(new LambdaQueryWrapper<SunnyResultDetail>()
                .eq(SunnyResultDetail::getResultId, resultId));

        //区分一下新的模块，如果读到的数据的moduleName不是重复的，就重新创建一个sheet页
        Set<String> moduleSet = new HashSet<>();
        ExcelUtils resultExcel = new ExcelUtils();
        //每次都从第2行开始写起
        int rowNo = 1;
        //遍历每一条详情数据
        for (SunnyResultDetail sunnyResultDetail : sunnyResultDetails) {
            //先判断一下是否是新的moduleName
            if (!moduleSet.contains(sunnyResultDetail.getModuleName())) {
                //不包含的话，创建sheet页，同时还需要往set里存一下这个名字
                moduleSet.add(sunnyResultDetail.getModuleName());
                resultExcel.createSheet(sunnyResultDetail.getModuleName());
                //每次创建完之后，从第2行开始进行写入
                rowNo = 1;
            }
            //把数据转换成list格式
            List<String> rowContent = new ArrayList<>() {{
                //判断一下是不是用例行，不是用例行的话，写空字符串
                add(sunnyResultDetail.getParentId() == null ? sunnyResultDetail.getCaseName() : "");
                add(sunnyResultDetail.getStepName());
                add(sunnyResultDetail.getKeywords());
                add(sunnyResultDetail.getParam1());
                add(sunnyResultDetail.getParam2());
                add(sunnyResultDetail.getStatus().name());
                add(sunnyResultDetail.getDetail());
            }};
            //区分用例行和步骤行进行写入
            if (sunnyResultDetail.getParentId() == null || sunnyResultDetail.getParentId() <= 0) {
                resultExcel.writeLine(rowNo, "case", rowContent);
                //重新设置结果列的样式
                resultExcel.setStatusCellStyle(rowNo, 5, "case", sunnyResultDetail.getStatus().name());
            } else {
                resultExcel.writeLine(rowNo, "step", rowContent);
                //重新设置结果列的样式
                resultExcel.setStatusCellStyle(rowNo, 5, "step", sunnyResultDetail.getStatus().name());
            }
            //行号自增
            rowNo++;
        }

        return resultExcel.getWorkbook();

    }
}
